package com.flow.filter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.flow.model.PermissionMenu;
import com.flow.model.RolePermission;
import com.flow.model.UserRole;
import com.flow.service.IRolePermissionService;
import com.flow.service.IUserRoleService;
import com.flow.tools.StringTools;

public class ManagerFilter implements Filter {

	private FilterConfig config;

	private IRolePermissionService<RolePermission> rpService;

	private IUserRoleService<UserRole> urService;

	private static List<String> commonUrl = null;


	private void init() {

		commonUrl = new ArrayList<String>();
		commonUrl.add("manager/css");
		commonUrl.add("manager/js");
		commonUrl.add("manager/images");
		commonUrl.add("common/css");
		commonUrl.add("common/images");
		commonUrl.add("common/js");
		commonUrl.add(".jsp");
		commonUrl.add("manager_userbasic/login");

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {

		System.out.println("进入权限filter");
		chain.doFilter(req, resp);
		/*HttpServletRequest request = null;
		HttpServletResponse response = null;
		HttpSession session = null;
		String login = config.getInitParameter("managerLoginPage");

		try {

			init();
			request = (HttpServletRequest) req;
			response = (HttpServletResponse) resp;
			session = request.getSession();
			String requestPath = request.getServletPath();
			requestPath = requestPath.substring(requestPath.indexOf("/") + 1);
			System.out.println(requestPath);

			for (String url : commonUrl) {
				if (requestPath.contains(url)) {
					chain.doFilter(req, resp);
					return;
				}
			}

			Object adminId = session.getAttribute("ADMINID");

			if (null == adminId) {
				response.sendRedirect(login);
				return;
			}

			List<UserRole> urList = findUserRoleByUbId(adminId.toString());

			if (null == urList || urList.isEmpty()) {
				response.sendRedirect(login);
				return;
			}

			List<RolePermission> rpList = findRolePermission(urList);

			if (null == rpList || rpList.isEmpty()) {
				response.sendRedirect(login);
				return;
			}

			for (RolePermission rp : rpList) {
				if (requestPath.contains(rp.getPermissionMenu().getPmNamespace())) {
					for (String str : getPermission(rp)) {
						if(requestPath.contains(str)){
							chain.doFilter(req, resp);
							return;
						}
					}
					
				}

			}
			
			response.sendRedirect("error.jsp");
			return;

		} catch (Exception e) {

			e.printStackTrace();

		}
*/
	}

	public List<String> getPermission(RolePermission rp){
		
		List<String> p = new ArrayList<String>();
		if(null == rp)
			return null;
		
		if(0 == rp.getRpFind())
			p.add("find");
		
		if(0 == rp.getRpInsert())
			p.add("insert");
		
		if(0 == rp.getRpDelete())
			p.add("delete");
		
		if(0 == rp.getRpUpdate())
			p.add("update");
		
		return p;
		
	}
	
	public List<RolePermission> findRolePermission(List<UserRole> urList) {

		if (null == urList || urList.isEmpty())
			return null;

		try {

			List<Object> rtIdList = new ArrayList<Object>();
			for (UserRole userRole : urList) {
				if(null != userRole && null != userRole.getRoleTab())
					rtIdList.add(userRole.getRoleTab().getRtId());
			}

			return rpService.findRolePermissionByrtIdList(rtIdList);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;

	}

	public List<UserRole> findUserRoleByUbId(String ubId) {

		if (!StringTools.nullOrEmpty(ubId))
			return null;

		try {

			return urService.findUserRoleByUbId(ubId);

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;

	}

	@Override
	public void init(FilterConfig config) throws ServletException {

		this.config = config;
		/*
		 * ServletContext context = config.getServletContext();
		 * ApplicationContext ctx =
		 * WebApplicationContextUtils.getWebApplicationContext(context); try {
		 * urService = ctx.getBean("UserRoleService"); } catch (Exception e) {
		 * // TODO: handle exception }
		 */

	}

	@Override
	public void destroy() {

		config = null;

	}

	public void setRpService(IRolePermissionService<RolePermission> rpService) {
		this.rpService = rpService;
	}

	public void setUrService(IUserRoleService<UserRole> urService) {
		this.urService = urService;
	}

}
